package com.rfsp.mcp.controller.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.rfsp.common.base.ResultVO;
import com.rfsp.common.util.FileUtil;
import com.rfsp.common.util.StringUtil;
import com.rfsp.mcp.pojo.po.FinanceMiInfoPO;
import com.rfsp.mcp.pojo.sdto.FinanceMiInfoSDTO;
import com.rfsp.mcp.service.FinanceMiInfoService;

@Controller("BaseFinanceMiInfoController")
@RequestMapping("/base/financeMiInfo")
public class BaseFinanceMiInfoController extends BaseController {
    
    /**上传地址*/
    @Value("${mcp.file-pdf-path-finance}")
    String filePDFPath;
    
    @Autowired
    FinanceMiInfoService financeMiInfoService;


    @GetMapping("/detail")
    public String detail(ModelMap model, FinanceMiInfoSDTO financeMiInfoSDTO) {
    	if(StringUtils.isBlank(financeMiInfoSDTO.getMiNo())) {
    		return this.errorAidNotMatch(model);
    	}
    	
    	FinanceMiInfoPO financeMiInfoPO = financeMiInfoService.getByMiNo(financeMiInfoSDTO.getMiNo());
    	
    	
    	financeMiInfoPO.setFilePath("/mcp/base/financeMiInfo/download/?miNo="+financeMiInfoPO.getMiNo());
        
        ResultVO<FinanceMiInfoPO> result = ResultVO.success(financeMiInfoPO);
        model.put("vo", result);
        
        
        return "base/finance/detail";
    }
    
    @RequestMapping("/download")
    public String download(HttpServletRequest request, HttpServletResponse response, ModelMap model,String miNo) {
    	
    	FinanceMiInfoPO financeMiInfoPO = financeMiInfoService.getByMiNo(miNo);
    	
        
        try {
			FileUtil.downloadfile(request, response, financeMiInfoPO.getFilePath(), financeMiInfoPO.getMiNo() + ".pdf");
		} catch (Exception e) {
			e.printStackTrace();
		}
        

        return null;
    }
    

    @GetMapping("/pdf")
    public String pdf(ModelMap model,Long id) {
        return "base/finance/detail";
    }
}